package com.personal.website.lottery.web.service.impl;

import com.personal.website.lottery.web.dao.UserMapper;
import com.personal.website.lottery.web.model.User;
import com.personal.website.lottery.web.model.UserExample;
import com.personal.website.lottery.web.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Gavin on 2016/1/24.
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    public User getUserById(int id) {
        return this.userMapper.selectByPrimaryKey(id);
    }

    public List<User> getUserByUserName(String userName) {
        UserExample example = new UserExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return this.userMapper.selectByExample(example);
    }

    public List<User> getUserByMobilePhoneNo(String mobilePhoneNo) {
        UserExample example = new UserExample();
        example.createCriteria().andMobilePhoneNoEqualTo(mobilePhoneNo);
        return this.userMapper.selectByExample(example);
    }

    public List<User> getUserByEmail(String email) {
        UserExample example = new UserExample();
        example.createCriteria().andEmailEqualTo(email);
        return this.userMapper.selectByExample(example);
    }

    public void insertUser(User user) {
        this.userMapper.insert(user);
    }


    public boolean updateUser(User user) {
        return this.userMapper.updateByPrimaryKey(user) == 1;
    }

    public Boolean validateUserInfo(String userName, String email) {
        UserExample userEaxmple = new UserExample();
        userEaxmple.createCriteria().andEmailEqualTo(email).andUserNameEqualTo(userName);
        return this.userMapper.countByExample(userEaxmple) == 1;
    }

    public boolean deleteUser(int userId) {
        return this.userMapper.deleteByPrimaryKey(userId) == 1;
    }

    public List<User> getUserLikeUserName(String text, int limit) {
        UserExample userExample = new UserExample();
        userExample.setLimitLength(limit);
        userExample.setOrderByClause("register_time desc");
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUserNameLike(text);
        return this.userMapper.selectByExample(userExample);
    }
}
